//注册路由的处理函数
//导入数据库连接
const db = require('../db/index')

module.exports.register = (req, res) => {
  userInfo = req.body
  const sqlStr = 'SELECT * FROM Administer WHERE username=?'
  db.query(sqlStr, userInfo.username, (err, result) => {
    //注册失败
    if (err) {
      return res.send({
        status: 1,
        message: '注册失败'
      })
    }
    //注册成功  1.用户被占用  2.直接注册成功
    if (result.length === 1) {
      return res.send({
        status: 1,
        message: '注册失败,用户已被注册'
      })
    }
    if (result.length === 0) {
      const sqlStrInster = 'INSERT INTO Administer(username,passworld) VALUES(?,?)'
      db.query(sqlStrInster, [userInfo.username, userInfo.passworld], (err, result) => {
        if (err) {
          return res.send({
            status: 1,
            message: '注册失败'
          })
        }
        if (result.affectedRows === 1) {
          return res.send({
            status: 200,
            message: '注册成功'
          })
        }
      })
    }
  })
}
